package com.accompany.api.presenter;
import com.accompany.api.bean.UploadNichengMsgBean;
import com.accompany.api.category.UploadNichengMsgCategory;
import com.accompany.utils.RetrofitUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.File;
import java.io.IOException;
import java.util.List;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 信息说明:
 * company: 兰州丰荣信息科技有限公司
 * 创建时间: on 2022/4/20 10 ：54
 * 项目名称：yuanban
 */
public class UploadNichengMsgPresenter implements UploadNichengMsgCategory.Presenter {
    UploadNichengMsgCategory.View view;

    public UploadNichengMsgPresenter(UploadNichengMsgCategory.View view) {
        this.view = view;
    }

    @Override
    public void uploadImage(long userId, String mToken, String path) {
        File file = new File(path);
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
        if (file !=null){
            RequestBody body=RequestBody.create(MediaType.parse("multipart/form-data"), file);//表单类型
            builder.addFormDataPart("file", file.getName(),body);
        }
        List<MultipartBody.Part> parts=builder.build().parts();
        RetrofitUtil.getInstance().getmService().uploadImage(mToken,userId,parts).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String result = null;
                try {
                    result = response.body().string();
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    UploadNichengMsgBean userInfor = gson.fromJson(result, UploadNichengMsgBean.class);
                    view.uploadImage(userInfor);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                view.OnShowErrorMsg(t.getMessage());
            }
        });
    }

    @Override
    public void uploadImageNicheng(String mToken,String requestBody) {
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestBody);
        RetrofitUtil.getInstance().getmService().get_upload_nicheng(mToken,body).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String result = null;
                try {
                    result = response.body().string();
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    UploadNichengMsgBean userInfor = gson.fromJson(result, UploadNichengMsgBean.class);
                    view.uploadImageNicheng(userInfor);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                view.OnShowErrorMsg(t.getMessage());
            }
        });
    }
}
